<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>APIHelpers</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        
        <h1>
            <span class="type">Module</span>
            APIHelpers
            
        </h1>
        <ul class="files">
            
            <li><a href="../files/app/api/api_helpers_rb.html">app/api/api_helpers.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-authenticate-21">authenticate!</a>,
              </li>
            
              
              <li>
                <a href="#method-i-authenticate_user-21">authenticate_user!</a>,
              </li>
            
              
              <li>
                <a href="#method-i-authorization">authorization</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-current_user">current_user</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>E</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-encode_params_values">encode_params_values</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-normalize_encode_params">normalize_encode_params</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-authenticate-21">
            
              <b>authenticate!</b>()
            
            <a href="../classes/APIHelpers.html#method-i-authenticate-21" name="method-i-authenticate-21" class="permalink">Link</a>
          </div>

          
            <div class="description">
              <p>认证用户</p>
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-authenticate-21_source')" id="l_method-i-authenticate-21_source">show</a>
                
              </p>
              <div id="method-i-authenticate-21_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 3</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">authenticate!</span>
  <span class="ruby-comment"># 如果token不存在，则返回没有得到token</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">APIErrors</span><span class="ruby-operator">::</span><span class="ruby-constant">NoGetAuthenticate</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">headers</span>[<span class="ruby-string">&quot;Authorization&quot;</span>].<span class="ruby-identifier">present?</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">APIErrors</span><span class="ruby-operator">::</span><span class="ruby-constant">AuthenticateFail</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">current_user</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-authenticate_user-21">
            
              <b>authenticate_user!</b>()
            
            <a href="../classes/APIHelpers.html#method-i-authenticate_user-21" name="method-i-authenticate_user-21" class="permalink">Link</a>
          </div>

          
            <div class="description">
              
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-authenticate_user-21_source')" id="l_method-i-authenticate_user-21_source">show</a>
                
              </p>
              <div id="method-i-authenticate_user-21_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 24</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">authenticate_user!</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">APIErrors</span><span class="ruby-operator">::</span><span class="ruby-constant">AuthenticateFail</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">current_user</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-authorization">
            
              <b>authorization</b>()
            
            <a href="../classes/APIHelpers.html#method-i-authorization" name="method-i-authorization" class="permalink">Link</a>
          </div>

          
            <div class="description">
              <p>设备的sn码或者是用户名</p>
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-authorization_source')" id="l_method-i-authorization_source">show</a>
                
              </p>
              <div id="method-i-authorization_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 10</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">authorization</span>
  <span class="ruby-ivar">@header_authorization</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">headers</span>[<span class="ruby-string">&quot;Authorization&quot;</span>]
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-current_user">
            
              <b>current_user</b>()
            
            <a href="../classes/APIHelpers.html#method-i-current_user" name="method-i-current_user" class="permalink">Link</a>
          </div>

          
            <div class="description">
              
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-current_user_source')" id="l_method-i-current_user_source">show</a>
                
              </p>
              <div id="method-i-current_user_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 20</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">current_user</span>
  <span class="ruby-ivar">@current_user</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">ApiToken</span>.<span class="ruby-identifier">authenticate</span>(<span class="ruby-identifier">authorization</span>).<span class="ruby-identifier">try</span>(<span class="ruby-value">:store_staff</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-encode_params_values">
            
              <b>encode_params_values</b>(hash)
            
            <a href="../classes/APIHelpers.html#method-i-encode_params_values" name="method-i-encode_params_values" class="permalink">Link</a>
          </div>

          
            <div class="description">
              <p>迭代对 params 的 values 进行编码处理</p>
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-encode_params_values_source')" id="l_method-i-encode_params_values_source">show</a>
                
              </p>
              <div id="method-i-encode_params_values_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 29</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">encode_params_values</span>(<span class="ruby-identifier">hash</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">k</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
      <span class="ruby-identifier">encode_params_values</span>(<span class="ruby-identifier">hash</span>[<span class="ruby-identifier">k</span>])
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">k</span>].<span class="ruby-identifier">force_encoding</span>(<span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UTF_8</span>).<span class="ruby-identifier">encode!</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">k</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-normalize_encode_params">
            
              <b>normalize_encode_params</b>()
            
            <a href="../classes/APIHelpers.html#method-i-normalize_encode_params" name="method-i-normalize_encode_params" class="permalink">Link</a>
          </div>

          
            <div class="description">
              <p>对客户端提交的未正确进行UTF-8编码的数据进行编码</p>
            </div>
          

          

          

          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-normalize_encode_params_source')" id="l_method-i-normalize_encode_params_source">show</a>
                
              </p>
              <div id="method-i-normalize_encode_params_source" class="dyn-source">
                <pre><span class="ruby-comment"># File app/api/api_helpers.rb, line 15</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">normalize_encode_params</span>
  <span class="ruby-ivar">@env</span>[<span class="ruby-string">&quot;rack.request.form_hash&quot;</span>] <span class="ruby-operator">||=</span> {}
  <span class="ruby-identifier">encode_params_values</span> <span class="ruby-identifier">params</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">form_data?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">media_type</span> <span class="ruby-operator">==</span> <span class="ruby-string">&#39;multipart/form-data&#39;</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>
